package fun.asgc.boot.netty.web.context;

import com.google.common.collect.Sets;
import fun.asgc.boot.netty.annotation.*;
import fun.asgc.boot.netty.bean.factory.SimpleBeanFactory;
import fun.asgc.boot.netty.context.ApplicationContext;
import fun.asgc.boot.netty.context.ApplicationRunner;
import fun.asgc.boot.netty.web.HttpRequestHandler;
import fun.asgc.boot.netty.web.WebApplicationServer;
import fun.asgc.boot.netty.web.interceptor.ControllerGlobalInterceptor;
import fun.asgc.boot.netty.web.router.DefaultHttpRouter;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author: aoshiguchen
 * @date: 2022/7/15
 */
@Accessors(chain = true)
@Data
@Slf4j
@NonIntercept
@Component
public class WebApplicationContext implements ApplicationRunner {
	@Autowired
	private ApplicationContext applicationContext;
	@Autowired
	private SimpleBeanFactory applicationBeanFactory;
	/**
	 * bean工厂
	 */
	private SimpleBeanFactory webApplicationBeanFactory;

	@Init
	public void init() {
		this.webApplicationBeanFactory = new SimpleBeanFactory(applicationBeanFactory, "webApplicationBeanFactory");
		this.webApplicationBeanFactory.init();
	}

	@Destroy
	public void destroy() {
		this.webApplicationBeanFactory.destroy();
	}

	@Override
	public void run(String[] args) throws Exception {
		this.webApplicationBeanFactory.register(
			Sets.newHashSet(
				DefaultHttpRouter.class,
				HttpRequestHandler.class,
				ControllerGlobalInterceptor.class,
				WebApplicationServer.class
			)
		);
	}

}
